package handler

import (
	"fitness/api/client"
	"fitness/api/request"
	fitness "fitness/common/proto"
	"fitness/pkg"
	"github.com/gin-gonic/gin"
)

func UserLogin(c *gin.Context) {
	var data request.UserLogin
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}

	login, err := client.UserLogin(c, &fitness.UserLoginReq{
		Username: data.Username,
		Password: data.Password,
		Phone:    data.Mobile,
	})
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}
	claims := pkg.CustomClaims{ID: uint(login.Greet)}
	token, err := pkg.NewJWT("2209A").CreateToken(claims)
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"data": token,
	})
}
func AddReservation(c *gin.Context) {
	var data request.AddReservation
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}
	userId := c.GetUint("userId")
	reservation, err := client.AddReservation(c, &fitness.AddReservationReq{
		ClassName: data.ClassName,
		AddTime:   data.AddTime,
		Address:   data.Address,
		Teacher:   data.Teacher,
		Context:   data.Context,
		UserId:    int64(userId),
	})
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"data": reservation,
	})
}
func ReservationList(c *gin.Context) {
	userId := c.GetUint("userId")
	list, err := client.ReservationList(c, &fitness.ReservationListReq{UserId: int64(userId)})

	if err != nil {
		c.JSON(200, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"data": list,
	})
}

func FitnessList(c *gin.Context) {
	var data request.FitnessList
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}
	list, err := client.FitnessList(c, &fitness.FitnessListReq{FitnessId: data.FitnessId})
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"data": list,
	})
}
func FindUserReservation(c *gin.Context) {
	var data request.FindUserReservation
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}
	reservation, err := client.FindUserReservation(c, &fitness.FindUserReservationReq{
		Username: data.Username,
		Status:   data.Status,
	})
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"data": reservation,
	})
}
